package com.homelock.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.homelock.common.controller.BasicController;
import com.homelock.entity.HlDevUser;
import com.homelock.entity.HlFamilyLinkage;
import com.homelock.service.HlFamilyLinkageService;
import com.homelock.service.LockUserService;
import com.homelock.vo.DevFamilLinkageVo;
import com.pi.base.dto.result.AppResult;
import com.pi.base.util.time.DateTools;

@RequestMapping("/lock/linkage/family")
@RestController
public class HlFamilyLinkageController extends BasicController {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(HlFamilyLinkageController.class);

	private HlFamilyLinkageService familyLinkageService;
	
	private LockUserService lockUserService;

	@Autowired
	public HlFamilyLinkageController(HlFamilyLinkageService familyLinkageService, LockUserService lockUserService) {
		Assert.notNull(familyLinkageService, "锁具用户亲情服务为空");
		Assert.notNull(lockUserService, "锁具用户服务为空");
		this.familyLinkageService = familyLinkageService;
		this.lockUserService = lockUserService;
	}

	@RequestMapping("/add")
	public AppResult addfamilyMember(Long devId, Long userId, Long targetUserId) {
		Long linkageSeq = familyLinkageService.add(devId, userId, targetUserId);
		return AppResult.newSuccessResult(linkageSeq.toString());
	}

	@RequestMapping("/remove")
	public AppResult removefamilyMember(Long devId, Long userId, Long targetUserId) {
		familyLinkageService.removeone(devId, userId, targetUserId);
		return AppResult.OK;
	}

	@RequestMapping("/clean")
	public AppResult cleanfamilyMember(Long devId) {
		familyLinkageService.removeAll(devId);
		return AppResult.OK;
	}

	@RequestMapping("/updateRelation")
	public AppResult updateLockUser(Long devId, Long userId, Long targetUserId) {
		familyLinkageService.updateUserUnlockName(devId, userId, targetUserId);
		return AppResult.OK;
	}
	
	@RequestMapping("/selectOneinfo")
	public AppResult selectOneinfo(Long devId, Long userId, Long targetUserId) {
	  HlFamilyLinkage familyLinkageInfo = familyLinkageService.getFamilyLinkageInfo(devId, userId, targetUserId);
	  if(null == familyLinkageInfo) {
	    return AppResult.OK;
	  }
    List<Long> lockUserIds = new ArrayList<>(2);
    lockUserIds.add(userId);
    lockUserIds.add(targetUserId);
    List<HlDevUser> devUserList = lockUserService.queryLockUserListByIds(devId, lockUserIds);
    if(CollectionUtils.isEmpty(devUserList)) {
      return AppResult.OK;
    }
    Map<Long, String> devUserMap = devUserList.stream().collect(Collectors.toMap(HlDevUser::getId, HlDevUser::getDevUserName));
    DevFamilLinkageVo familyLinkageVo = new DevFamilLinkageVo();
    familyLinkageVo.setCreateDate(DateTools.getFullDate(familyLinkageInfo.getCreateDate()));
    familyLinkageVo.setId(familyLinkageInfo.getId().toString());
    familyLinkageVo.setTargetUserId(targetUserId.toString());
    familyLinkageVo.setTargetUserName(devUserMap.getOrDefault(targetUserId, StringUtils.EMPTY));
    familyLinkageVo.setUserId(userId.toString());
    familyLinkageVo.setUserName(devUserMap.getOrDefault(userId, StringUtils.EMPTY));
    return AppResult.newSuccessResult(familyLinkageVo);
	}

	@RequestMapping("/list")
	public AppResult queryFamilyLinkageList(Long devId) {
		List<HlFamilyLinkage> familyLinkageList = familyLinkageService.getallInfo(devId);
		if(CollectionUtils.isEmpty(familyLinkageList)) {
		  return AppResult.OK;
		}
		List<Long> lockUserIds = new ArrayList<>(familyLinkageList.size() * 2);
		for(HlFamilyLinkage hlFamilyLinkage : familyLinkageList) {
		  if(!lockUserIds.contains(hlFamilyLinkage.getUserId())) {
		    lockUserIds.add(hlFamilyLinkage.getUserId());
		  }
		  if(!lockUserIds.contains(hlFamilyLinkage.getTargetUserId())) {
		    lockUserIds.add(hlFamilyLinkage.getTargetUserId());
		  }
		}
    List<HlDevUser> devUserList = lockUserService.queryLockUserListByIds(devId, lockUserIds);
    if(CollectionUtils.isEmpty(devUserList)) {
      return AppResult.OK;
    }
    Map<Long, String> devUserMap = devUserList.stream().collect(Collectors.toMap(HlDevUser::getId, HlDevUser::getDevUserName));
    List<DevFamilLinkageVo> list = new ArrayList<DevFamilLinkageVo>();
    for(HlFamilyLinkage hlFamilyLinkage : familyLinkageList) {
      DevFamilLinkageVo familyLinkageVo = new DevFamilLinkageVo();
      familyLinkageVo.setCreateDate(DateTools.getFullDate(hlFamilyLinkage.getCreateDate()));
      familyLinkageVo.setId(hlFamilyLinkage.getId().toString());
      familyLinkageVo.setTargetUserId(hlFamilyLinkage.getTargetUserId().toString());
      familyLinkageVo.setTargetUserName(devUserMap.getOrDefault(hlFamilyLinkage.getTargetUserId(), StringUtils.EMPTY));
      familyLinkageVo.setUserId(hlFamilyLinkage.getUserId().toString());
      familyLinkageVo.setUserName(devUserMap.getOrDefault(hlFamilyLinkage.getUserId(), StringUtils.EMPTY));
      list.add(familyLinkageVo);
    }
    return AppResult.newSuccessResult(list);
	}
}
